package export.monster
{
	import base.BaseMonster;
	
	import com.dusk.util.McUtil;
	import com.dusk.util.RandomUtil;
	import com.dusk.util.ResUtil;
	import com.game.data.constant.FrameRate;
	import com.game.engine.MainGame;
	import com.game.manager.AnimationManager;
	import com.greensock.TweenMax;
	
	import export.bullet.RegionBullet;
	
	import export.bullet.SpecialEffectBullet;
	import export.level.StageListener41;
	
	import flash.display.Bitmap;
	import flash.display.MovieClip;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	public class Monster404 extends BaseMonster
	{
		//牛魔王
		
		private var m104BG:Bitmap;
		
		private var leftHand:MovieClip;
		
		private var rightHand:MovieClip;
		
		private var leftHandSpeed:int;
		
		private var rightHandSpeed:int;
		
		private var isAtking:Boolean = false;
		
		private var leftEye:MovieClip;
		
		private var rightEye:MovieClip;
		
		public function Monster404()
		{
			super();
		}
		
		override protected function initBBDC() : void
		{
			bbdc = AnimationManager.getAnimation("Monster404Action");
			bbdc.setAction("idle");
			bbdc.turnRight();
			bbdc.addCallBack(onEnterFrame, onActionOver);
			bodyContainer.addChild(bbdc);
		}
		
		override protected function initAttribute():void
		{
			super.initAttribute();
			gravity = 0;
			hide();
		}
		
		private function show() : void
		{
			visible = true;
			TweenMax.delayedCall(10,function(_self:Monster404):*
			{
				_self.hide();
			},[this]);
		}
		
		private function hide() : void
		{
			setInvincible(FrameRate.ANIMATE_FRAME_RATE * 10);
			visible = false;
			TweenMax.delayedCall(10,function(_self:Monster404):*
			{
				_self.show();
			},[this]);
		}
		
		override protected function initCollideArea() : void
		{
			collideBox = new Rectangle(-75,-75,150,150);
			super.initCollideArea();
		}
		
		override protected function onAdded() : void
		{
			super.onAdded();
			var p:Point = gc.gameScene.globalToLocal(new Point(470,380));
			x = p.x;
			y = p.y;
			m104BG = ResUtil.getImageBitmap("Monster404BG") as Bitmap;
			m104BG.x = x - 470;
			m104BG.y = y - 315;
			var idx1:int = gc.gameScene.getChildIndex(this);
			gc.gameScene.addChildAt(m104BG,idx1);
			leftHand = ResUtil.getNewObj("Monster404Hand") as MovieClip;
			leftHand.x = x - 200;
			leftHand.y = y - 50;
			gc.gameScene.addChild(leftHand);
			rightHand = ResUtil.getNewObj("Monster404Hand") as MovieClip;
			McUtil.flipHorizontal(rightHand,-1);
			rightHand.x = x + 200;
			rightHand.y = y - 50;
			gc.gameScene.addChild(rightHand);
			rightEye = ResUtil.getNewObj("Monster404Eyes") as MovieClip;
			rightEye.x = x + 15;
			rightEye.y = y - 170;
			gc.gameScene.addChild(rightEye);
			leftEye = ResUtil.getNewObj("Monster404Eyes") as MovieClip;
			leftEye.x = x - 45;
			leftEye.y = y - 170;
			McUtil.flipHorizontal(leftEye,-1);
			gc.gameScene.addChild(leftEye);
		}
		
		override protected function attackTarget() : void
		{
			isAtking = true;
			var tarHand:MovieClip = RandomUtil.randBoolean() ? leftHand : rightHand;
			var b:SpecialEffectBullet = new SpecialEffectBullet("Monster404Bullet1");
			b.setOwner(this);
			b.setPosition(tarHand.x,tarHand.y);
			b.setAction("Monster404Attack1");
			b.setHurt(getActionHurt("attack"));
			b.setIsDestroyWhenLastFrame(false);
			b.setDestroyInCount(36);
			b.setDirect(bbdc.getDirect());
			gc.gameScene.addChild(b);
			magicBulletArray.push(b);
			doHit1(tarHand);
		}
		
		private function attackDone():void
		{
			if(isReadyToDestroy) return;
			setAction("idle");
			isAtking = false;
		}
		
		override public function setAction(str:String) : void
		{
			if(str != "dead" && str != "idle" && str != "hit1")
			{
				str = "idle";
			}
			super.setAction(str);
		}
		
		override protected function onActionOver(action:String) : void
		{
			super.onActionOver(action);
			switch(action)
			{
				case "dead":
					if(getHp() <= 0 && MainGame.getIns().levelData.levelID == "4_1")
					{
						TweenMax.delayedCall(5,function(s:StageListener41):void
						{
							s.showAnimate();
						},[gc.pWorld.getBaseLevelListener() as StageListener41]);
					}
					if(m104BG)
					{
						TweenMax.to(m104BG,1,{
							"alpha":0,
							"onComplete":function(_bm:Bitmap):*
							{
								_bm.bitmapData.dispose();
								_bm = null;
							},
							"onCompleteParams":[m104BG]
						});
						break;
					}
			}
		}
		
		override public function turnLeft() :void
		{
		}
		
		override public function turnRight() :void
		{
		}
		
		override protected function beforeSkill1Start() : Boolean
		{
			return true;
		}
		
		override protected function releSkill1() : void
		{
			for(var i:int = 0; i < RandomUtil.randInt(4,8); i++)
			{
				doHit2();
			}
		}
		
		private function doHit1(mc:MovieClip) : void
		{
			TweenMax.to(mc,2,{"y":y - 50 + 150});
			TweenMax.to(mc,2,{"y":y - 50,"delay":3,"onComplete":attackDone});
		}
		
		private function doHit2() : void
		{
			var b:SpecialEffectBullet = new SpecialEffectBullet("Monster404Bullet2");
			var p:Point = gc.gameScene.globalToLocal(new Point(RandomUtil.randInt(100,800),650));
			b.setOwner(this);
			b.setPosition(p.x,p.y);
			b.setDirect(bbdc.getDirect());
			b.setAction("Monster404Skill1");
			b.setHurt(getActionHurt("skill1"));
			gc.gameScene.addChild(b);
			magicBulletArray.push(b);
		}
		
		override protected function myIntelligence() : void
		{
			renderHand();
			if(!isBeAttacking())
			{
				super.myIntelligence();
			}
		}
		
		private function renderHand():void
		{
			if(_framer % FrameRate.ANIMATE_FRAME_RATE == 0)
			{
				if(gc.gameScene.localToGlobal(new Point(leftHand.x,0)).x < 40)
				{
					leftHandSpeed = 2;
				}
				else if(gc.gameScene.localToGlobal(new Point(leftHand.x,0)).x > 400)
				{
					leftHandSpeed = -2;
				}
				else
				{
					leftHandSpeed = Math.random() * 10 - Math.random() * 10;
				}
				/*----------------------------设置两只手的位移-----------------------------*/
				if(gc.gameScene.localToGlobal(new Point(leftHand.x,0)).x < 40)
				{
					leftHandSpeed = 2;
				}
				else if(gc.gameScene.localToGlobal(new Point(leftHand.x,0)).x > 400)
				{
					leftHandSpeed = -2;
				}
				else
				{
					leftHandSpeed = Math.random() * 2 - Math.random() * 2;
				}
			}
			if(!isAtking)
			{
				rightHand.x += rightHandSpeed;
				leftHand.x += leftHandSpeed;
			}
			else
			{
				leftHandSpeed = rightHandSpeed = 0;
			}
		}
		
		override public function destroy() : void
		{
			super.destroy();
			if(leftHand)
			{
				leftHand.parent.removeChild(leftHand);
				leftHand = null;
			}
			if(rightHand)
			{
				rightHand.parent.removeChild(rightHand);
				rightHand = null;
			}
			if(leftEye)
			{
				leftEye.parent.removeChild(leftEye);
				leftEye = null;
			}
			if(rightEye)
			{
				rightEye.parent.removeChild(rightEye);
				rightEye = null;
			}
		}
	}
}
